<template>
  <div>
    <el-dialog
      @open="resetForm"
      :title="isAdd ? `新增${isLabel}` : `修改${isLabel}`"
      :visible="visible"
      @update:visible="$emit('update:visible', $event)"
      width="450px"
      :before-close="handleClose">

      <el-form ref="dataForm" :model="dataForm" label-width="80px" :rules="rules" v-model="loading">
         <el-form-item label="所属学科" prop="subjectID">
          <el-select v-model="dataForm.subjectID" placeholder="请选择所属学科">
            <el-option
              v-for="(item, index) in subList"
              :key="index"
              :label="item.subjectName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="tagName" label="标签名称" v-if="isLabel==='标签'">
          <el-input v-model="dataForm.tagName"></el-input>
        </el-form-item>
        <el-form-item prop="directoryName" label="目录名称" v-if="isLabel==='目录'">
          <el-input v-model="dataForm.directoryName"></el-input>
        </el-form-item>
        <el-form-item class="dialog-footer">
          <el-button @click="$emit('update:visible', false)">取 消</el-button>
          <el-button type="primary" @click="onConfirm">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
    getSubjectList,
    addNewCatalogue,
    getCataDetail,
    updateCatalogue,
    addNewLabel,
    getLabelDetail,
    updateLabel
  } from '@/api/subjectmanage'

export default {
  name: 'LsDialog',
  props:{
    visible: Boolean,
    passId: Number,
    isAdd: Boolean,
    isLabel: String
  },
  data() {
    return {
      loading: false,
      dataForm: {
        directoryName: '',
        tagName: '',
        subjectID: null
      },
      rules: {
        directoryName: [{ required: true, trigger: 'blur'}],
        tagName: [{ required: true, trigger: 'blur'}],
        subjectID: [{ required: true, trigger: 'blur'}]
      },
      detailInfo: {},
      subList: [],
      params: {
        page: 1,
        pagesize: 999
      }
    }
  },

  methods: {
    handleClose(done) {
      return done()
    },
    // 重置表单
    resetForm() {
      this.$refs.dataForm.resetFields()
    },
    // 提交信息
    async onConfirm(){
      this.loading = true
      let res = ''
      if(this.isAdd) {
        if(this.isLabel==='目录'){
          res = await addNewCatalogue({
            directoryName: this.dataForm.directoryName,
            subjectID: this.dataForm.subjectID
          })
        }else {
          res = await addNewLabel({
            id: this.passId,
            tagName: this.dataForm.tagName,
            subjectID: this.dataForm.subjectID
          })
        }
      }else {
        if (this.isLabel === '目录'){
          res = await updateCatalogue(this.passId, {
            id: this.passId,
            directoryName: this.dataForm.directoryName,
            subjectID: this.dataForm.subjectID
          })
        }else {
          res = await updateLabel(this.passId, {
            id: this.passId,
            tagName: this.dataForm.tagName,
            subjectID: this.dataForm.subjectID
          })
        }
      }
      this.loading = false
      if(res) {
        this.$message({
          message: this.isAdd ? '添加成功' :'修改成功',
          type: 'success'
        })
      }else {
        this.$message.error(this.isAdd ? '添加失败' :'修改失败')
      }
      this.$emit('success')
      this.$emit('update:visible', false)
    },
    // 获取学科列表
    async getSubList() {
      const res = await getSubjectList(this.params)
      this.subList = res.items
    },
    // 获取目录详情
    async getCatalogueDe(id) {
      const res = await getCataDetail(id)
      this.dataForm.directoryName = res.directoryName
      this.dataForm.subjectID = res.subjectID
    },
    // 获取标签详情
    async getLabelDet(id) {
      this.loading = true
      await getLabelDetail(id)
      this.dataForm.tagName = res.tagName
      this.dataForm.subjectID = res.subjectID
      this.loading = false
    }
  },

  created(){
    this.getSubList()
  },

  watch: {
    visible(val) {
      if(val && this.passId && !this.isAdd){
        if (this.isLabel === '目录') {
          this.getCatalogueDe(this.passId)
        }else {
          this.getLabelDet(this.passId)
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.article-info{
  font-size: 12px;
  color: #888;
}
.context-box{
  width: 760px;
}

.article-info{
  padding-left: 8px;
  span{
    margin-right: 8px;
  }
}
.dialog-footer{
  display: flex;
  justify-content: flex-end;
}
::v-deep .el-select{
  width: 100%;
}
</style>
